mui.init();
//控制图片大小
var max = document.documentElement.clientWidth;

function initImg(listimg) {
	listimg.parentNode.style.height = (max - 31) / 3.0 + "px";
}
var marketId = localStorage.marketId;
var checkining = false;

var demo = new Vue({
	el: '#main',
	data: {
		banners: [],
		sorts: [{
			'id': marketId,
			'name': '全部'
		}],
		pictureAd: '',
		textList: [],
		circles: []
	},
	methods: {
		init: init,
		openGuanzhu:openGuanzhu
	}
});

document.getElementById(marketId).className = 'active';
//轮播图、图片、文字广告
var urlBanner = getUrl("75getAdvert");
var paramsBanner = {
	"advertLevel": "market",
	"marketId": marketId
};
demo.$http({
	url: urlBanner,
	method: 'POST',
	data: paramsBanner,
	params: paramsBanner
}).then(function(response) {
	//轮播图
	if (response.data.lunList.tops[0]) {
		var str = response.data.lunList.tops[0].images;
		demo.banners = str.split(',');
		setTimeout(function() {
			var mySwiper = new Swiper('.swiper-container', {
				direction: 'horizontal',
				loop: true,
				autoplay: 5000,
				speed: 1500,
				pagination: '.swiper-pagination'
			});
		}, 1000);
	}
	//图片广告
	if (response.data.picList.mids[0]) {
		demo.pictureAd = response.data.picList.mids[0].images;
	}
	//文字广告
	if (response.data.textList) {
		$scope.textList = response.data.textList.mids;
		setTimeout(function() {
			new Slide("gundong", "neirong", 2000).init();
		}, 3000)
	}
}, function(response) {
	console.log('广告出错' + JSON.stringify(response));
});
//*圈子分类*//
var paramsSort = {
	"marketId": marketId
};
var urlSort = getUrl("74getCatalogs");
demo.$http({
	url: urlSort,
	method: 'POST',
	data: paramsSort,
	params: paramsSort
}).then(function(response) {
	//alert(JSON.stringify(response.data));
	var lists = response.data.obj.systemcataloglist;
	for (var i = 0; i < response.data.obj.systemcataloglist.length; i++) {
		demo.sorts.push(lists[i]);
	}
}, function(response) {
	console.log('圈子分类错误' + JSON.stringify(response));
});

/*读取圈子*/
var pagesAll = 0;
var pages, params1, params2, typeId;
var urlMore = getUrl("70marketCircle");
var nowSort = marketId;
demo.init(marketId);

function init(mainId) {
	if (nowSort != mainId) {
		document.getElementById(nowSort).className = "";
		document.getElementById(mainId).className = "active";
		nowSort = mainId;
	}
	if (mainId == marketId) {
		typeId = null;
		params1 = {
			"indexpage": 0,
			"pageSize": 5,
			id: mainId
		}
	} else {
		typeId = mainId;
		params1 = {
			"indexpage": 0,
			"pageSize": 5,
			id: marketId,
			"catalogID": mainId
		}
	}
	pages = 0;
	demo.$http({
		url: urlMore,
		method: 'POST',
		data: params1,
		params: params1
	}).then(function(response) {
		//alert(JSON.stringify(response.data));
		demo.circles = [];
		var crcls = response.data.obj.list;
		for (var i = 0; i < crcls.length; i++) {
			for (var pic in crcls[i].productList) {
				if (!crcls[i].productList[pic]["picture"]) {
					crcls[i].productList[pic]["picture"] = "../../image/shopping/small.png"
				}
			}
			demo.circles.push(crcls[i]);
		}
		pages++;
		pagesAll = response.data.obj.totalpage;
		mui('#pullrefresh').pullRefresh().refresh(true);
	}, function(response) {
		console.log('读取圈子出错' + JSON.stringify(response.data.obj));
	});
}
//上啦加载
mui.init({
	pullRefresh: {
		container: '#pullrefresh',
		up: {
			contentrefresh: '正在加载...',
			callback: pullupRefresh
		}
	}
});

function pullupRefresh() {
	if (pages < pagesAll) {
		setTimeout(function() {
			loadMore();
		}, 1000);
	} else {
		mui('#pullrefresh').pullRefresh().endPullupToRefresh(true); //参数为true代表没有更多数据了
	}
}

function loadMore() {
	if (!typeId) {
		params2 = {
			"indexpage": pages,
			"pageSize": 5,
			id: marketId
		}
	} else {
		params2 = {
			"indexpage": pages,
			"pageSize": 5,
			id: marketId,
			"catalogID": typeId
		}
	}

	demo.$http({
		url: urlMore,
		method: 'POST',
		data: params2,
		params: params2
	}).then(function(response) {
		var crcls = response.data.obj.list;
		for (var i = 0; i < crcls.length; i++) {
			for (var pic in crcls[i].productList) {
				if (!crcls[i].productList[pic]["picture"]) {
					crcls[i].productList[pic]["picture"] = "../../image/shopping/small.png"
				}
			}
			demo.circles.push(crcls[i]);
		}
		pages++;
		mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
	}, function(response) {
		console.log('下拉加载出错')
	});
}

/*if (mui.os.plus) {
	mui.plusReady(function() {
		setTimeout(function() {
			mui('#pullrefresh').pullRefresh().pullupLoading();
		}, 1000);

	});
} else {
	mui.ready(function() {
		mui('#pullrefresh').pullRefresh().pullupLoading();
	});
}*/

//打开圈子详情
function openGuanzhu(gid, groupId) {
	localStorage.guideId = writeData(gid);
	localStorage.youHId = writeData(groupId);
	mui.openWindow({
		url: 'guanzhu.html',
		id: 'guanzhu.html'
	});
}
//读取签到状态
if (localStorage.account) {
	var urlcheckStatus = getUrl("77getSignStatus");
	var headerCheckStatus = getHeader();
	var checkStatusParams = "accountId=" + readData(localStorage.account) + "&marketId=" + marketId;
	demo.$http({
		url: urlcheckStatus,
		method: 'POST',
		headers: headerCheckStatus,
		data: checkStatusParams,
		params: checkStatusParams,
		timeout: 10000
	}).then(function(response) {
		if (response.data.code == 0) {
			if (response.data.status == 1) {
				document.getElementById("checkin").src = "../../image/shopping/marketcheck1.png";
				checkining = true;
			}
		}
	}, function(response) {
		console.log('签到出错' + JSON.stringify(response));
	});
}
/*签到*/
function check() {
	if (!localStorage.account) {
		mui.openWindow({
			url: '../my/loginHead.html',
			id: 'loginHead.html'
		});
		return;
	}
	var success = document.getElementById("success");
	var checkin = document.getElementById("checkin");
	var urlcheck = getUrl("76checkin");
	var headerCheck = getHeader();
	var checkParams = "accountId=" + readData(localStorage.account) + "&scoreCatalogCode=1&marketId=" + marketId;
	if (!checkining) {
		checkining = true;
		demo.$http({
			url: urlcheck,
			method: 'POST',
			headers: headerCheck,
			data: checkParams,
			params: checkParams
		}).then(function(response) {
			//alert(JSON.stringify(response.data));
			checkin.src = "../../image/shopping/marketcheck1.png";
			success.className = "checkInSuccess animated zoomIn";
			setTimeout(function() {
				success.className = "checkInSuccess animated zoomOut";
			}, 2000)
		}, function(response) {
			console.log('签到出错' + JSON.stringify(response.data.obj));
		});
	} else {
		mui.toast('亲,今天已签到过啦，明天再来吧~');
	}
}